//
//  VisitorView.swift
//  新浪微博
//
//  Created by MBP on 16/5/11.
//  Copyright © 2016年 MBP. All rights reserved.
//

import UIKit
import SnapKit

protocol VisitorViewDelegate: NSObjectProtocol {
    //访客 注册
    func visitorViewDidRegister()
    //访客 登录
    func visitorViewDidLogin()
}
class VisitorView: UIView {

    //代理属性 weak
    weak var delegate: VisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //调用设置访客视图的方法
        setupUI()
        //注册界面上 注册 / 登录 按钮
        registerButton.addTarget(self, action: #selector(VisitorView.clickRigester), forControlEvents: .TouchUpInside)
        loginButton.addTarget(self, action: #selector(VisitorView.clickLogin), forControlEvents: .TouchUpInside)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        print("访客视图 deinit")
    }
    
    
    //设置访客视图信息, 在子控制器中重写该方法, 实现各自的需求
    func setupInfo(imageName: String? = nil, message: String) {
        messageLabel.text = message
        
        //守护: iconView是nil, 表示为首页
        guard let imageName = imageName else{
            //开始动画
            startAnimation()
            return
        }
        //如果不是首页, 需要隐藏小房子和遮盖视图
        homeIconView.hidden = true
        maskIconView.hidden = true
        //设置各自的图标
        iconView.image = UIImage(named: imageName)
    }
    
    //首页展示动画的方法
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //对于需要循环播放的动画, 设置该属性为false,视图离开屏幕后，不会被销毁！当 动画所在视图被销毁后，动画会连带销毁, 不需要考虑释放问题
        anim.removedOnCompletion = false
        
        //加入到layer中
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    //懒加载所需空间
    //图标
    private lazy var iconView: UIImageView = UIImageView(pn_imageName: "visitordiscover_feed_image_smallicon")
    //遮盖
    private lazy var maskIconView: UIImageView = UIImageView(pn_imageName: "visitordiscover_feed_mask_smallicon")
    //小房子
    private lazy var homeIconView: UIImageView = UIImageView(pn_imageName: "visitordiscover_feed_image_house")
    //消息label
    private lazy var messageLabel: UILabel = UILabel(pn_text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", alignment: .Center)
        
    //注册按钮
    private lazy var registerButton: UIButton = UIButton(
        pn_title: "注册",
        titleColor: UIColor.orangeColor(),
        backImageName: "common_button_white_disable")
    
    //登录按钮
    private lazy var loginButton: UIButton = UIButton(
        pn_title: "登录",
        titleColor: UIColor.orangeColor(),
        backImageName: "common_button_white_disable")
 
    
    //按钮的点击事件
    @objc private func clickRigester() {
        print("点击了注册")
        delegate?.visitorViewDidRegister()
    }
    @objc private func clickLogin() {
        print("点击了登录")
        delegate?.visitorViewDidLogin()
        
    }
    
   
}

extension VisitorView {
    private func setupUI() {
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //设置文字对齐方式
        messageLabel.textAlignment = .Center
        
        //设置控件的自动布局
        //图标
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        //遮盖视图
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
        //房子
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        //消息lable
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 224, height: 36))
        }
        //注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.left.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        //登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        //空白区域使用背景色, 使主题色保持一致
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
}
